<template>
  <el-dialog
    title="提示"
    :visible.sync="assignRolePup"
    width="70%"
    @close="$emit('close')"
  >
    <el-checkbox-group v-model="roleIds" @change="change">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="$emit('close')">取 消</el-button>
          <el-button type="primary" @click="assignRole">确 定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getEmployeeBaseInfo } from '@/api/user.js'
import { getAllRole } from '@/api/setting'
import { assignRole } from '@/api/employee'
export default {
  name: 'AssignRoles',
  props: {
    assignRolePup: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      rolesList: [],
      roleIds: []
    }
  },
  created() {
    this.getAllRole()
  },
  methods: {
    async getAllRole() {
      const res = await getAllRole()
      this.rolesList = res.rows
    },
    // 获取员工基本信息
    async getEmployeeBaseInfo(id) {
      const res = await getEmployeeBaseInfo(id)
      this.roleIds = res.roleIds
    },
    // 分配角色
    async assignRole() {
      await assignRole({
        id: this.id,
        roleIds: this.roleIds
      })
      this.$message('分配成功')
      this.$emit('close')
      this.roleIds = []
    },
    change(a) {
      this.roleIds = a
    }
  }
}
</script>

<style></style>
